/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.client;

import net.bioteam.appweb.gwt.apps.client.Util.ApplicationCallBack;
import net.bioteam.appweb.gwt.apps.client.Util.LinkLabel;
import net.bioteam.appweb.gwt.apps.client.program.AppTreeGrid;
import net.bioteam.appweb.gwt.apps.clientandserver.user.UserAction;
import net.bioteam.common.user.User;

import com.google.gwt.user.client.History;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.HTMLFlow;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.LayoutSpacer;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.toolbar.ToolStrip;

public class MainPanel extends VLayout
{

	Label userLabel;
	LinkLabel userButton;
	LinkLabel dsLabel;

	public MainPanel()
	{
		setWidth100();
		setHeight100();

		// -------------TOP
		HTMLFlow top = new HTMLFlow();
		top.setAutoHeight();
		top.setContentsURL("header.html");
		top.setStyleName("myhead");
		// top.setBorder("2px solid red");
		addMember(top);

		// ------------------tool bar
		ToolStrip bar = new ToolStrip();
		bar.setWidth100();
		bar.setHeight(20);
		bar.setAlign(Alignment.RIGHT);
		bar.setMembersMargin(5);

		Label statusLabel = new Label();
		statusLabel.setWidth(200);
		statusLabel.setBackgroundColor("yellow");
		statusLabel.setVisible(false);
		bar.addMember(statusLabel);
		StaticHolder.statusLabel = statusLabel;

		userLabel = new Label();
		userLabel.setStyleName("userlabel");
		userLabel.setPrompt("Your username");
		userLabel.setAlign(Alignment.RIGHT);
		userLabel.setWidth(80);
		bar.addMember(userLabel);

		userButton = new LinkLabel("Users");
		userButton.setVisible(false);
		userButton.setWidth(30);
		userButton.addClickHandler(new ClickHandler()
		{

			public void onClick(ClickEvent event)
			{
				History.newItem("admin_users");

			}
		});
		bar.addMember(userButton);

		dsLabel = new LinkLabel("DS");
		dsLabel.setVisible(false);
		dsLabel.setWidth(20);
		dsLabel.addClickHandler(new ClickHandler()
		{

			public void onClick(ClickEvent event)
			{
				History.newItem("admin_ds");

			}
		});
		bar.addMember(dsLabel);

		LinkLabel jobButton = new LinkLabel("Jobs");
		jobButton.setWidth(30);
		jobButton.addClickHandler(new ClickHandler()
		{

			public void onClick(ClickEvent event)
			{
				History.newItem("user_jobs");

			}
		});
		bar.addMember(jobButton);

		LinkLabel dataButton = new LinkLabel("Data");
		dataButton.setWidth(30);
		dataButton.addClickHandler(new ClickHandler()
		{

			public void onClick(ClickEvent event)
			{
				History.newItem("user_data");

			}
		});
		bar.addMember(dataButton);

		Label account = new LinkLabel("Account");
		account.setWidth(50);
		account.addClickHandler(new ClickHandler()
		{

			public void onClick(ClickEvent event)
			{
				History.newItem("user_account");

			}
		});
		bar.addMember(account);

		LinkLabel templateButton = new LinkLabel("Templates");
		templateButton.setWidth(60);
		templateButton.addClickHandler(new ClickHandler()
		{

			public void onClick(ClickEvent event)
			{
				History.newItem("user_templates");

			}
		});
		bar.addMember(templateButton);

		LinkLabel analysisButton = new LinkLabel("Analysis");
		analysisButton.setWidth(55);
		analysisButton.addClickHandler(new ClickHandler()
		{

			public void onClick(ClickEvent event)
			{
				History.newItem("user_analysis_1");

			}
		});
		analysisButton.setPrompt("Data-based analysis");
		bar.addMember(analysisButton);

		LinkLabel qstatButton = new LinkLabel("QStat");
		qstatButton.setWidth(30);
		qstatButton.addClickHandler(new ClickHandler()
		{

			public void onClick(ClickEvent event)
			{
				History.newItem("user_qstat");

			}
		});
		qstatButton.setPrompt("SGE queue stat");
		bar.addMember(qstatButton);

		LinkLabel about = new LinkLabel("About");
		about.setWidth(30);
		about.addClickHandler(new ClickHandler()
		{

			public void onClick(ClickEvent event)
			{
				SC.say("Open iNquiry is From <a href='http://www.bioteam.net'>BioTeam</a>." + "BioTeam provides consulting for" + "<uo><li>HPC Cluster Building and Management for Bio-Computing</li>"
						+ "	<li>Next Gen Sequencer Data Management</li>" + "	<li>Cloud-based Application Enabling</li>" + "<li>wikiLiMS:flexible LIMS for biological research</li>" + "</uo>");
			}
		});
		bar.addMember(about);

		LinkLabel logout = new LinkLabel("Logout");
		logout.setWidth(45);
		logout.addClickHandler(new ClickHandler()
		{

			public void onClick(ClickEvent event)
			{
				StaticHolder.loginPanel.logout();
			}
		});
		bar.addMember(logout);

		LayoutSpacer spacer = new LayoutSpacer();
		spacer.setWidth(100);
		bar.addMember(spacer);

		addMember(bar);

		// -------------------center
		HLayout center = new HLayout();
		center.setWidth100();
		center.setHeight("*");
		// center.setResizeBarClass(resizeBarClass)

		StaticHolder.appTreeGrid = new AppTreeGrid();
		center.addMember(StaticHolder.appTreeGrid);

		ContentPanel content = new ContentPanel();
		StaticHolder.content = content;
		center.addMember(content);

		addMember(center);

		// --------------footer
		HTMLFlow footer = new HTMLFlow();
		footer.setAutoHeight();
		// footer.setBorder("2px solid green");
		footer.setStyleName("myfooter");
		footer.setContentsURL("footer.html");
		addMember(footer);
	}

	boolean inited = false;

	public void init()
	{
		if (inited)
			return;
		ApplicationCallBack<User> callback = new ApplicationCallBack<User>()
		{

			public void onFailureAfter(Throwable caught)
			{
				SC.warn(caught.getMessage(), StaticHolder.dumpB);
			}

			public void onSuccessAfter(User result)
			{

				userLabel.setContents("[ hello " + result.getName() + "]");
				if (result.getRole().equals("admin"))
				{
					userButton.show();
					dsLabel.show();

				}
				StaticHolder.user = result;
			}

			public void execute()
			{
				UserAction.Util.getInstance().getLoginUser(this);
			}

		};
		callback.execute();
	}

}
